import java.io.*;
import java.text.*;
import java.util.ArrayList;
import java.util.Calendar;

import javax.xml.parsers.*;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class IpokerHandParser extends HandParser {

	public IpokerHandParser(String uri, String name) {
		super(uri, name);
	}

	@Override
	public GameInfo ReadFile(String file) 
	{
		DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
		
        DocumentBuilder db = null;
        try {
            db = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException pce) {
            System.err.println(pce);
            System.exit(1);
        }
        
        Document doc = null;
        try {
            doc = db.parse(new File(file));
        } catch (SAXException se) {
            System.err.println(se.getMessage());
            System.exit(1);
        } catch (IOException ioe) {
            System.err.println(ioe);
            System.exit(1);
        }
        GameInfo gi=new GameInfo();
        
        
        //date
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		gi.date=Calendar.getInstance();
		try {
			gi.date.setTime(df.parse(doc.getElementsByTagName("startdate").item(0).getTextContent()));
		} catch (DOMException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		//edate
		gi.edate=Calendar.getInstance();
		try {
			gi.edate.setTime(df.parse(doc.getElementsByTagName("startdate").item(doc.getElementsByTagName("startdate").getLength()-1).getTextContent()));
		} catch (DOMException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
        
		//gamenum
		gi.gamenum=Long.parseLong(doc.getElementsByTagName("session").item(0).getAttributes().item(0).getTextContent());
		
		//duration
		gi.duration=Calendar.getInstance();
		gi.duration.setTimeInMillis(gi.edate.getTimeInMillis()-gi.date.getTimeInMillis());
		
		String temp=doc.getElementsByTagName("buyin").item(0).getTextContent();
		//buy-in
		gi.buyin=Double.parseDouble(temp.substring(1, temp.indexOf("+")));
		
		//rake
		gi.rake=Double.parseDouble(temp.substring(temp.indexOf("+")+2));
		
		short h=0;
		NodeList nl=doc.getElementsByTagName("players").item(0).getChildNodes();
		//players
		for(int i=0;i<nl.getLength();i++)
			if(nl.item(i).getNodeName().compareTo("player")==0)
				h++;
		gi.players=h;
		
		temp=doc.getElementsByTagName("win").item(0).getTextContent();
		//winnings
		gi.winnings=Double.parseDouble(temp.substring(1));
		
		//finishing
		temp=doc.getElementsByTagName("place").item(0).getTextContent();
		gi.finishing=Short.parseShort(temp);
		
		//roomname
		gi.roomname=this.getName();
		
		return gi;
	}

	@Override
	public ArrayList<GameInfo> Reader() {
		ArrayList<GameInfo> lista=new ArrayList<GameInfo>();
		File maindir=new File(Uri+"/Tournament");
		
		try {
			for(String s: maindir.list())
			{
				if(s.contains(".xml"))
					lista.add(this.ReadFile(maindir.getPath()+"/"+s));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		for(int i=0;i<lista.size();i++)
			if(lista.get(i)==null)
				lista.remove(i);
		
		return lista;
	}

}
